#include "bt_package/BtNode.h"
#include <fstream>
#include <plog/Log.h>
#include <string>

using namespace std;

BtNode::BtNode(const char *nodeName, std::string config_path) : Node(nodeName) {
  ifstream readf(config_path);
  if (readf.is_open()) {
    LOGI << "the file:" << config_path << " open ok.";
  } else {
    LOGE << "please check the file " << config_path << " whether exists!!";
  }

  try {
    nlohmann::json config_json;
    readf >> config_json;

    _port_name = config_json.value("port_name", "/dev/ttyS0");
    _baud_rate = config_json.value("baud_rate", 9600);

    exec_thread_ = std::thread(&BtNode::execThread, this);
  } catch (const json::exception &e) {

    LOGE << "json exception:" << e.what();
  }
}

void BtNode::execThread() {
  while (rclcpp::ok()) {
        LOGI<<"hello ros2 world";
        sleep(50);
  }
}


void BtNode::joinExec_Thread(){
        exec_thread_.join();
}